package fileoperation;

import repository.Repository;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

public class FileStatus {
    /**
     * To check if the branch exists.
     * @param branchname
     * @return
     * @throws FileNotFoundException
     */
    public static boolean branchExist(String branchname)throws FileNotFoundException {
        File branch = new File(Repository.getGitDir() + File.separator + "refs"
                + File.separator + "heads" + File.separator + branchname);

        return branch.isFile();
    }

    /**
     * Compare the timestamp of the file to see if it is changed.
     * @param path
     * @param filename
     * @param indexList
     * @return
     * @throws FileNotFoundException
     */
    public static boolean fileChanged(String path, String filename, LinkedList<String[]> indexList) throws FileNotFoundException{
        boolean change = false;
        File file = new File(path + File.separator + filename);
        if(!file.isFile()){
            throw new FileNotFoundException(filename + " is not a file!");
        }
        String timeStamp = new Date(file.lastModified()).toString();
        for(int i = 0; i < indexList.size(); i++){
            if(indexList.get(i)[2].equals(filename)){
                long time = Long.parseLong(indexList.get(i)[3]);
                Date date = new Date(time);
                String indexTime = date.toString();
                change = !indexTime.equals(timeStamp);
                break;
            }
        }
        return change;
    }
    public static List<File> workTreeList(String workdir) {
        return workTreeList(new File(workdir));
    }
    public static List<File> workTreeList(File workTree) {
        List<File> result = new ArrayList<File>();
        if (workTree.listFiles() == null) {//防止主函数调用时给出路径不是一个有效的目录
            System.out.println("NOT A DIRECTORY");
            return result;//直接返回空的list
        }

        for(File item : workTree.listFiles()) {
            if(item.isDirectory()) {
                if(item.getName().equals(".jit")) //正则匹配工作区
                    continue;
                if(item.getName().matches("branch|commander|core|fileoperation|gitobject|repository|sha1|stage|zlib|utils|test"))
                    continue;
                result.addAll(workTreeList(item));
            } else {
                result.add(item);
            }
        }

        return result;
    }
    public static List<File> objectsList(String objects) {
        return objectsList(new File(objects));
    }
    public static List<File> objectsList(File objects) {
        List<File> result = new ArrayList<File>();
        if (objects.listFiles() == null) {//防止主函数调用时给出路径不是一个有效的目录
            System.out.println("NOT A DIRECTORY");
            return result;//直接返回空的list
        }

        for(File item : objects.listFiles()) {
            if(item.isDirectory()) {
                if(item.getName().matches("info|pack"))
                    continue;
                result.addAll(workTreeList(item));
            } else {
                result.add(item);
            }
        }
        return result;
    }
}
